/**
 * 简单倒计时
 * const endTime = '2022-01-01';
 * console.log(countdown(endTime));
 * @param {*} endTime
 * @returns
 */
export function countdown(endTime) {
  const endDate = new Date(endTime).getTime()
  const now = new Date().getTime()
  const distance = endDate - now

  const days = Math.floor(distance / (1000 * 60 * 60 * 24))
  const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60))
  const seconds = Math.floor((distance % (1000 * 60)) / 1000)
  return `${days} 天 ${hours} 小时 ${minutes} 分钟 ${seconds} 秒`
}

/**
 * JavaScript 倒计时函数，可以设置倒计时结束后的回调函数：
 * const endTime = '2022-01-01'
 * countdown(endTime, () => {
 *    console.log('倒计时结束！')
 * })
 * @param {*} endTime
 * @param {*} callback
 */
export function countdownCallBack(endTime, callback) {
  const endDate = new Date(endTime).getTime()
  const intervalId = setInterval(() => {
    const now = new Date().getTime()
    const distance = endDate - now
    if (distance <= 0) {
      clearInterval(intervalId)
      callback()
    } else {
      const days = Math.floor(distance / (1000 * 60 * 60 * 24))
      const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
      const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60))
      const seconds = Math.floor((distance % (1000 * 60)) / 1000)
      return `${days} 天 ${hours} 小时 ${minutes} 分钟 ${seconds} 秒`
    }
  }, 1000)
}

/**
 * 表格时间格式化
 * @param {*} cellValue
 * @returns
 */
export function formatDate(cellValue) {
  if (cellValue == null || cellValue === '') return ''
  const date = new Date(cellValue)
  const year = date.getFullYear()
  const month = date.getMonth() + 1 < 10 ? `0${date.getMonth() + 1}` : date.getMonth() + 1
  const day = date.getDate() < 10 ? `0${date.getDate()}` : date.getDate()
  const hours = date.getHours() < 10 ? `0${date.getHours()}` : date.getHours()
  const minutes = date.getMinutes() < 10 ? `0${date.getMinutes()}` : date.getMinutes()
  const seconds = date.getSeconds() < 10 ? `0${date.getSeconds()}` : date.getSeconds()
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

/**
 * 时间格式化
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (`${time}`.length === 10) {
    time = parseInt(time, 10) * 1000
  } else {
    time = +time
  }
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  }
  if (diff < 3600) {
    // less 1 hour
    return `${Math.ceil(diff / 60)}分钟前`
  }
  if (diff < 3600 * 24) {
    return `${Math.ceil(diff / 3600)}小时前`
  }
  if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  }
  return `${d.getMonth() + 1}月${d.getDate()}日${d.getHours()}时${d.getMinutes()}分`
}

/**
 * 数组转树结构
 * @param {*} arr
 * @param {*} pid
 * @returns
 */
export function arrayToTree(arr, pid) {
  return arr.reduce((res, current) => {
    if (current['pid'] === pid) {
      current.children = arrayToTree(arr, current['id'])

      return res.concat({
        id: current.id,
        pid: current.pid,
        name: current.name,
        children: current.children,
      })
    }
    return res
  }, [])
}

/**
 *把若干数组按指定的字段名进行分组
 * @param {*} list
 * @param {*} propName
 * @returns
 */
export function groupBy(list, propName) {
  return list.reduce((acc, item) => {
    const key = item[propName]
    if (!acc[key]) {
      acc[key] = []
    }
    acc[key].push(item)
    return acc
  }, {})
}
